/*
 * @(#)PicturePanel.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.panels;

import org.eoti.swing.event.FutureActionListener;
import org.eoti.swing.ScrollablePicture;
import org.eoti.swing.Ruler;
import static org.eoti.swing.Ruler.Orientation.*;
import static org.eoti.swing.Ruler.Unit.*;
import static org.eoti.swing.Ruler.*;
import static org.eoti.swing.border.BorderUtil.*;
import static org.eoti.awt.WebColor.*;

import static javax.swing.JScrollPane.*;
import static javax.swing.ScrollPaneConstants.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class PicturePanel
		extends JPanel
		implements ActionListener
{
	protected Ruler colRuler, rowRuler;
	protected JButton btnUnits;
	protected ScrollablePicture picture;
	protected Ruler.Unit btnState;

	public PicturePanel(ImageIcon icon)
	{
		this(icon, 35);
	}

	public PicturePanel(ImageIcon icon, int thickness)
	{
		super();

        colRuler = new Ruler(Horizontal, Inch, thickness);
		colRuler.setPreferredSize(icon);

		rowRuler = new Ruler(Vertical, Inch, thickness);
		rowRuler.setPreferredSize(icon);

		picture = new ScrollablePicture(icon, colRuler.getDotsPerIncrement());

		JPanel pnlCorner = colRuler.createMatchingCorner();
		btnState = Inch;
		btnUnits = new JButton(" ");
		setBtnText();
		btnUnits.addActionListener(new FutureActionListener(this));
		btnUnits.setFont(picture.getFont());
		btnUnits.setMargin(new Insets(2,2,2,2));
		pnlCorner.add(btnUnits);

		JScrollPane picSP = new JScrollPane(picture);
		picSP.setPreferredSize(new Dimension(picture.getSize()));
		picSP.setViewportBorder(createLinedBorder(Black));
		picSP.setColumnHeader(colRuler);
		picSP.setRowHeader(rowRuler);
		picSP.setCorner(UPPER_LEFT_CORNER, pnlCorner);
		picSP.setCorner(LOWER_LEFT_CORNER, colRuler.createMatchingCorner());
		picSP.setCorner(UPPER_RIGHT_CORNER, colRuler.createMatchingCorner());
		picSP.setCorner(LOWER_RIGHT_CORNER, colRuler.createMatchingCorner());

		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		add(picSP);
		setBorder(createPaddedBorder(20)); // base on Font?
	}

	private void setBtnText()
	{
		btnUnits.setText(btnState.getAbbreviation());
	}

	public void setIcon(ImageIcon icon)
	{
		picture.setIcon(icon);
		colRuler.setPreferredSize(icon);
		rowRuler.setPreferredSize(icon);
	}

	public void setUnits(Ruler.Unit unit)
	{
		colRuler.setUnits(unit);
		rowRuler.setUnits(unit);
		btnState = unit;
		setBtnText();
	}

	public void nextUnit()
	{
		Unit[] units = Unit.values();
		boolean next = false;
		for(Unit unit : units)
		{
			if(next)
			{
				setUnits(unit);
				return;
			}

			if(unit == btnState)
				next = true;
		}

		setUnits(units[0]);
	}

	public void actionPerformed(ActionEvent e)
	{
		nextUnit();
	}

	public void setFont(Font font)
	{
		picture.setFont(font);
		btnUnits.setFont(font);
	}

	public Font getFont()
	{
		if(picture == null) // init'ing LNF
			return new Font("SansSerif", Font.PLAIN, 10);

		return picture.getFont();
	}


}
